def num_zero_and_five_digits(n):
count =0while n >0:
digit = n % 10if digit ==0or digit ==5:
count = count + 1
n = n // 10return count
6
In 4.2, tmpstring = str(thenumber) so if we want to count count many words in a number there is an easy way for example:
print(tmpstring.count("3"))
7
How to open python document:
For Mac: 应用程序--python --python documentation
8
How to leave space for integer/string/float:
%d--integer
%f--float
%s--string
ex:
print("%d * %d = %d"%(i,j,i*j),end="\t\t")
9
tips:
\t = tab
\n = newline
10
Print a multiplication table by using composition:
def print_oneline(i):
for j inrange(1,i+1):
print("%d * %d = %d"%(i,j,i*j),end="\t\t")def print_multiplicationTable(n):
for i inrange(1,n+1):
print_oneline(i)print()
11 program implements a simple guessing game:
importrandom# We cover random numbers in the
rng =random.Random()# modules chapter, so peek ahead.
number = rng.randrange(1,1000)# Get random number between [1 and 1000).
guesses =0
msg =""whileTrue:
guess =int(input(msg + "\nGuess my number between 1 and 1000: "))
guesses +=1if guess > number:
msg +=str(guess) + " is too high.\n"elif guess < number:
msg +=str(guess) + " is too low.\n"else:
breakinput("\n\nGreat, you got it in {0} guesses!\n\n".format(guesses))
12
dichotomie:
This divide the range in half and then divide the half into half and repeat this process until we get the number we want.
Input -- BOX -- Output
Input: arguments, global variables
Output: return, print, file(file storage in hardware)
2
Composition:
Call one function in another function
3
Boolean:
True and False
ex: a==b
4
Counting the digit of a number:
1
2
5
To count how many 5 and 0 in the number
6
In 4.2, tmpstring = str(thenumber) so if we want to count count many words in a number there is an easy way for example:
7
How to open python document:
For Mac: 应用程序--python --python documentation
8
How to leave space for integer/string/float:
%d--integer
%f--float
%s--string
ex:
9
tips:
\t = tab
\n = newline
10
Print a multiplication table by using composition:
11
program implements a simple guessing game:
12
dichotomie:
This divide the range in half and then divide the half into half and repeat this process until we get the number we want.
(the fastest way to find a number in a range)